package com.gobang.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gobang.domain.entity.ScoreRecord;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * 积分记录数据访问层
 * 
 * @author gobang
 */
@Mapper
public interface ScoreRecordMapper extends BaseMapper<ScoreRecord> {

    /**
     * 分页查询用户积分流水记录
     *
     * @param page 分页参数
     * @param userId 用户ID
     * @return 积分记录分页数据
     */
    @Select("SELECT sr.*, u.username as opponent_username " +
            "FROM score_records sr " +
            "LEFT JOIN users u ON sr.opponent_id = u.id " +
            "WHERE sr.user_id = #{userId} " +
            "ORDER BY sr.game_time DESC")
    IPage<ScoreRecord> findScoreRecordsByUserId(Page<ScoreRecord> page, @Param("userId") Long userId);

    /**
     * 查询用户总积分变化统计
     *
     * @param userId 用户ID
     * @return 总积分变化
     */
    @Select("SELECT COALESCE(SUM(score_change), 0) FROM score_records " +
            "WHERE user_id = #{userId}")
    Integer getTotalScoreChangeByUserId(@Param("userId") Long userId);

    /**
     * 查询用户胜场统计
     *
     * @param userId 用户ID
     * @return 胜场数
     */
    @Select("SELECT COUNT(1) FROM score_records " +
            "WHERE user_id = #{userId} AND game_result = 1")
    Integer getWinCountByUserId(@Param("userId") Long userId);

    /**
     * 查询用户总场数统计
     *
     * @param userId 用户ID
     * @return 总场数
     */
    @Select("SELECT COUNT(1) FROM score_records " +
            "WHERE user_id = #{userId}")
    Integer getTotalGameCountByUserId(@Param("userId") Long userId);
} 